package com.yz.aiks.multimodules;


import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * jedis基础工具类.
 *
 * @author <a href="mailto:aiks_yuan@163.com">yuanz</a>
 * @date 2022-02-24 14:02
 * @since 1.0.0
 **/
public abstract class AbstractJedisUtils {
    
    /**
     * 一分钟的缓存时间.
     */
    public static final Integer SECOND_MINUTER = 60;
    
    /**
     * 半小时秒数.
     */
    public static final int SECOND_HALF_HOURS = 60 * 30;
    
    /**
     * 一小时的秒数.
     */
    public static final int SECOND_HOURS = 60 * 60;
    
    /**
     * 一天的秒数.
     */
    public static final int SECOND_DAY = 60 * 60 * 24;
    
    /**
     * 一周的秒数.
     */
    public static final int SECOND_WEEK = 60 * 60 * 24 * 7;
    
    /**
     * 缓存5分钟.
     */
    public static final Integer FIVE_SECOND_MINUTER = 5 * SECOND_MINUTER;
    /**
     * 缓存两分钟
     */
    public static final Integer TWO_SECOND_MINUTER = 2 * SECOND_MINUTER;
    
    /**
     * 缓存8小时.
     */
    public static final Integer EIGHT_SECOND_HOURS = 8 * SECOND_HOURS;
    
    /**
     * redis缓存自增增量为空错误key.
     */
    public static final String REDIS_INCREMENT_NULL_ERROR_KEY = "redis.increment.not.null";
    
    /**
     * redis缓存key为blank错误key.
     */
    public static final String REDIS_KEY_BLANK_ERROR_KEY = "redis.key.not.blank";
    
    /**
     * redis缓存field为blank错误key.
     */
    public static final String REDIS_FIELD_BLANK_ERROR_KEY = "redis.field.not.blank";
    
    /**
     * redis缓存设置过期时间不能空.
     */
    public static final String REDIS_EXPIRE_TIME_NULL_ERROR_KEY = "redis.expire.time.not.null";
    
    private JedisPool jedisPool;
    
    /**
     * 设置连接池.
     *
     * @param jedisPool 不能为空.
     */
    public void setJedisPool(final JedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }
    
    /**
     * 借出jedis连接.
     */
    protected Jedis borrowConnection() {
        return jedisPool.getResource();
    }
    
    /**
     * 用完归还连接.
     */
    protected void revertConnection(final Jedis jedis) {
        jedis.close();
    }
    
}
